package com.models;

import com.collection.Bag;
import edu.princeton.cs.algs4.In;

public class EdgeWeightedGraph {
    private final int V;

    private int E;
    private Bag<Edge>[] adj;

    public EdgeWeightedGraph(int V) {
        this.V = V;
        this.E = 0;
        adj = (Bag<Edge>[]) new Bag[V];
        for (int i = 0; i < V; i++) {
            adj[i] = new Bag<>();
        }
    }
    public EdgeWeightedGraph(In in){
        this.V = in.readInt();
    };

    public void addEdge(Edge e) {
        int v = e.either();
        int w = e.other(v);
        adj[w].add(e);
        adj[v].add(e);
        E++;
    }

    public Iterable<Edge> adj (int v) {
        return adj[v];
    }

    public Iterable<Edge> edges() {

        Bag<Edge> b = new Bag<>();

        for (int v = 0; v < V; v++) {
            for (Edge e : adj(v)) {
                if (e.other(v) > v)
                    b.add(e);
            }
        }
        return b;
    }

    public int V() {

        return V;
    }
}
